package mnn;

/*
 This program is written by Ahmed Medhat Ahmed
 Egypt
 Alexandria Universty
 Faculty of Science
 Computer Science
 7-7-2007
 a.medhat.cs@gmail.com
 ama_compsc@yahoo.com
 */

import javax.swing.JOptionPane;

import no.hiof.imagepr.BinaryImage;
import no.hiof.imagepr.IntensityImage;
import no.hiof.imagepr.RGBImage;

public class ImageP {
	private int t;
	private int elem;
	private int input;
	private IntensityImage intensImage;
	private BinaryImage binImage;
	private int img_arr[];
	private int my_arr[];

	public ImageP() {

	}

	public int[] conv_to_data(String img_fils, int in) {
		t = 128;
		my_arr = new int[in];

		try {
			intensImage = new IntensityImage(new RGBImage(img_fils));
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Can't find the image",
					"Error in reading", JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
		binImage = new BinaryImage(intensImage, t);
		elem = binImage.getHeight() * binImage.getWidth();

		img_arr = new int[elem];

		for (int i = 0; i < elem; i++) {
			img_arr[i] = (binImage.getValueAt(i) ? 1 : 0);
		}
		input = elem / in;
		// JOptionPane.showMessageDialog(null,"Element ="+elem+"  input will contaian "+input);
		int sum = 0;
		int count = 0;
		for (int i = 0; i < elem; i++) {
			if ((i + 1) % input != 0) {
				sum += img_arr[i];
			} else {
				sum += img_arr[i];
				if (count < in)
					my_arr[count++] = (sum >= input / 2 ? 1 : 0);
				sum = 0;
			}
		}
		return my_arr;
	}
}